package cn.sciento.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;
import cn.sciento.wechat.dto.CreateAddConditionalMenuDTO;
import cn.sciento.wechat.dto.CreateAddConditionalMenuResultDTO;
import cn.sciento.wechat.dto.CreateMenuDTO;
import cn.sciento.wechat.dto.DefaultResultDTO;
import cn.sciento.wechat.dto.GetMenuConfigurationResultDTO;
import cn.sciento.wechat.dto.GetMenuResultDTO;
import cn.sciento.wechat.dto.TestMatchMenuResultDTO;
import cn.sciento.wechat.service.WeChatMenuService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class WeChatMenuServiceImp implements WeChatMenuService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public DefaultResultDTO createMenu(String accessToken, CreateMenuDTO createMenuDTO) {
    return (DefaultResultDTO)this.restTemplate.postForObject(" https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(createMenuDTO), (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public GetMenuResultDTO getMenu(String accessToken) {
    return (GetMenuResultDTO)this.restTemplate.getForObject("https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken, GetMenuResultDTO.class);
  }
  
  public DefaultResultDTO deleteMenu(String accessToken) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken, DefaultResultDTO.class);
  }
  
  public CreateAddConditionalMenuResultDTO createAddConditionalMenu(String accessToken, CreateAddConditionalMenuDTO createAddConditionalMenuDTO) {
    return (CreateAddConditionalMenuResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(createAddConditionalMenuDTO), (MultiValueMap)buildHttpHeaders()), CreateAddConditionalMenuResultDTO.class);
  }
  
  public DefaultResultDTO deleteAddConditionalMenu(String accessToken, String menuid) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(menuid), (MultiValueMap)buildHttpHeaders()), DefaultResultDTO.class);
  }
  
  public TestMatchMenuResultDTO testMatchMenu(String accessToken, String user_id) {
    return (TestMatchMenuResultDTO)this.restTemplate.postForObject("https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(user_id), (MultiValueMap)buildHttpHeaders()), TestMatchMenuResultDTO.class);
  }
  
  public GetMenuConfigurationResultDTO getMenuConfiguration(String accessToken) {
    return (GetMenuConfigurationResultDTO)this.restTemplate.getForObject("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken, GetMenuConfigurationResultDTO.class);
  }
  
  protected HttpHeaders buildHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "application/json");
    return httpHeaders;
  }
}
